<template>
  <div class="notice" style="padding: 15px">
    <div class="notice-form">
      <el-form :model="selectForm" :rules="dialogRules" ref="dialogForm" class="demo-dialogForm"
               inline>
        <el-form-item label="公告标题" prop="operIp">
          <el-input v-model="selectForm.noticeTitle" placeholder="请输入操作地址"></el-input>
        </el-form-item>
        <el-form-item label="操作人员" prop="operName">
          <el-input v-model="selectForm.createBy" placeholder="请输入操作人员"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="businessType">
          <el-select v-model="selectForm.noticeType" placeholder="操作类型" clearable>
            <el-option label="公告" value="2"></el-option>
            <el-option label="通知" value="1"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="selectButton">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>

      <el-button icon="el-icon-plus" plain type="primary" @click="insertHandler" size="small">新增</el-button>
    </div>
    <div class="notice-table">
      <el-table
          :data="notices"
          style="width: 100%">
        <el-table-column
            fixed
            prop="noticeId"
            label="序号"
            width="80">
        </el-table-column>
        <el-table-column
            prop="noticeTitle"
            label="公告标题"
            :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column
            prop="noticeType"
            label="公告类型"
            width="120">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.noticeType==='2'">公告</el-tag>
            <el-tag type="warning" v-else>通知</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="status"
            label="状态"
            width="120">
          <template slot-scope="scope">
            <el-tag :type=" scope.row.status==='0'?'':'danger' ">
              {{ scope.row.status === '0' ? '正常' : '关闭' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="createBy"
            label="创建者"
            width="120">
        </el-table-column>
        <el-table-column
            prop="createTime"
            label="创建时间"
            width="160">
        </el-table-column>
        <el-table-column
            fixed="right"
            label="操作"
            width="120">
          <template slot-scope="scope">
            <el-button @click="viewHandler(scope.row)" type="text" size="small" icon="el-icon-view">查看</el-button>
            <el-button @click="updateHandler(scope.row)" type="text" size="small" icon="el-icon-edit">修改</el-button>
            <el-button type="text" size="small" icon="el-icon-delete" @click="deleteHandler(scope)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="notice-page">
      <el-pagination
          background
          @size-change="SizeChangeHandler"
          @current-change="CurrentChangeHandler"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="PageSize"
          layout=" ->, total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <div>
      <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
        <!--ref相当于id-->
        <el-form :model="dialogForm" :rules="dialogRules" ref="dialogForm" label-width="100px">
          <el-form-item label="公告标题" prop="noticeTitle" placeholder="请输入公告标题">
            <el-input v-model="dialogForm.noticeTitle"></el-input>
          </el-form-item>
          <el-form-item label="公告类型" prop="noticeType">
            <el-select v-model="dialogForm.noticeType" placeholder="请选择公告类型" clearable>
              <el-option label="通知" value="1"></el-option>
              <el-option label="公告" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="dialogForm.status">
              <el-radio label="0">正常</el-radio>
              <el-radio label="1">关闭</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="内容" prop="noticeContent">
            <quill-editor v-model="dialogForm.noticeContent"></quill-editor>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible=false">取 消</el-button>
          <el-button type="primary" @click="dialogSubmitHandler">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import axios from 'axios'

const BASE_URL = "http://124.222.238.167/prod-api"
const TOKEN = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImExOGJhOGQ1LTc0NjctNDM2Yi1iMTJlLTYxNGYxNGRiMTk4NCJ9.YNWN5At-Jhvsrw52pgacGlN8Io3xgKGwVsA1x4Z2zhHfmmcSLQBNWUi8wgVHF69Nx3TVsDNYJPb832F5eB4jkg"
export default {
  name: "NoticeMange",
  data() {
    return {
      notices: [],
      currentPage:1,
      PageSize:10,
      total:0,
      dialogTitle: '修改公告',
      dialogVisible: false,
      dialogForm: {
        noticeType: "",
        noticeTitle: "",
        noticeContent: "",
        status: "0"
      },
      dialogMode: 'INSERT',
      dialogRules: {
        noticeTitle: [{
          required: true,
          message: '请输入公告标题',
          trigger: 'blur'
        }],
        noticeType: [{
          required: true,
          message: '请输入公告类型',
          trigger: 'change'
        }]
      },
      selectForm: {
        noticeTitle: '',
        createBy: '',
        noticeType: '',
      }
    }
  },
  created() {
    this.listNotice()
  },
  methods: {
    SizeChangeHandler(val){
      this.PageSize=val
      this.listNotice()
      console.log(val)
    },
    CurrentChangeHandler(val){
      this.currentPage=val
      this.listNotice()
      console.log(val)
    },
    insertHandler() {
      this.dialogMode = 'INSERT'
      this.dialogTitle = '新增公告'
      this.dialogVisible = true
      this.resetDialogForm()
    },
    updateHandler(data) {
      this.dialogMode = 'UPDATE'
      this.dialogTitle = '修改公告'
      this.dialogVisible = true
      this.resetDialogForm(data)
    },
    // 重置弹框
    resetDialogForm(data) {
      // 手动重置
      /* this.$options.data() 页面原始数据
         this.$data 最新的数据 */
      this.dialogForm = this.$options.data().dialogForm
      // callback
      this.$nextTick(() => {
        // element 重置
        this.$refs.dialogForm.resetFields()
        this.$refs.dialogForm.clearValidate()
        if (this.dialogMode === 'UPDATE') {
          this.dialogForm = JSON.parse(JSON.stringify(data))
        }
      })
    },
    editNotice() {
      axios({
        url: BASE_URL + '/system/notice',
        method: this.dialogMode === "INSERT" ? "POST" : "PUT",
        headers: {
          Authorization: TOKEN
        },
        data: this.dialogForm
      }).then(res => {
        const data = res.data
        if (data.code !== 200) {
          this.$message.error(data.msg)
          return
        }
        this.$message.success(data.msg || "新增成功")
        this.listNotice()
      }).catch(() => {
        this.$message.error("操作失败")
      }).finally(() => {
        this.dialogVisible = false
      })
    },
    dialogSubmitHandler() {
      // this.dialogVisible = true
      this.$refs.dialogForm.validate((flag, obj) => {
        if (!flag) {
          this.$message.error("参数校验失败")
          return false
        }
        this.editNotice()
      })
    },
    listNotice() {
      const params=JSON.parse(JSON.stringify(this.selectForm))
      params.pageNum=this.currentPage
      params.pageSize=this.PageSize

      axios({
        url: BASE_URL + "/system/notice/list",
        method: "GET",
        headers: {
          Authorization: TOKEN
        },
        params
      }).then(res => {
        // 异步请求成功后调用回调函数，callback
        const data = res.data
        if (data.code !== 200) {
          this.$message.error(data.msg)
          return
        }
        console.log(res)
        this.notices = data.rows
        this.total=data.total
      })
    },
    deleteHandler(data) {
      const id = data.row.noticeId
      axios({
        url: BASE_URL + "/system/notice/" + id,
        method: 'DELETE',
        headers: {
          Authorization: TOKEN
        }
      }).then(res => {
        const data = res.data
        console.log(data)
        if (data.code !== 200) {
          this.$message.error(data.msg)
          return
        }
        this.$message.success("删除成功")
        this.listNotice()
      })
    },
    selectButton() {
      this.listNotice()
    },
    // 重置按钮
    reset() {
      this.selectForm = {}
      this.listNotice()
    },
    viewHandler(data){
      // 编程式路由
      // this.$router.push('/system/notice/'+data.noticeId)
      this.$router.push({
        name:'systemDetail',
        // Path后面 路径变量
        params:{
          noticeId:data.noticeId,
          // noticeTitle:data.noticeTitle
        },
        // ?后面 参数
        query:{
          // noticeId:data.noticeId,
          noticeTitle:data.noticeTitle
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.el-select {
  width: 100%;
}
</style>